﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class UserShortcut : YJ.Data.Interface.IUserShortcut
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserShortcut()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.UserShortcut实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.UserShortcut model)
        {
            string sql = @"INSERT INTO UserShortcut
				(ID,MenuID,UserID,Sort) 
				VALUES(:ID,:MenuID,:UserID,:Sort)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = model.MenuID },
				new OracleParameter(":UserID", OracleDbType.Varchar2){ Value = model.UserID },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.UserShortcut实体类</param>
        public int Update(YJ.Data.Model.UserShortcut model)
        {
            string sql = @"UPDATE UserShortcut SET 
				MenuID=:MenuID,UserID=:UserID,Sort=:Sort
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = model.MenuID },
				new OracleParameter(":UserID", OracleDbType.Varchar2){ Value = model.UserID },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM UserShortcut WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.UserShortcut> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.UserShortcut> List = new List<YJ.Data.Model.UserShortcut>();
            YJ.Data.Model.UserShortcut model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.UserShortcut();
                model.ID = dataReader.GetString(0).ToGuid();
                model.MenuID = dataReader.GetString(1).ToGuid();
                model.UserID = dataReader.GetString(2).ToGuid();
                model.Sort = dataReader.GetInt32(3);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.UserShortcut> GetAll()
        {
            string sql = "SELECT * FROM UserShortcut";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM UserShortcut";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.UserShortcut Get(Guid id)
        {
            string sql = "SELECT * FROM UserShortcut WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByUserID(Guid userID)
        {
            string sql = "DELETE FROM UserShortcut WHERE UserID=:UserID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":UserID", OracleDbType.Varchar2){ Value = userID }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.UserShortcut> GetAllByUserID(Guid userID)
        {
            string sql = "SELECT * FROM UserShortcut WHERE UserID=:UserID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":UserID", OracleDbType.Varchar2){ Value = userID }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public DataTable GetDataTableByUserID(Guid userID)
        {
            string sql = "SELECT * FROM UserShortcut WHERE UserID=:UserID ORDER BY Sort";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":UserID", OracleDbType.Varchar2){ Value = userID }
			};
            return dbHelper.GetDataTable(sql, parameters);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByMenuID(Guid menuID)
        {
            string sql = "DELETE FROM UserShortcut WHERE MenuID=:MenuID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = menuID.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
    }
}